package com.mhys.hms.service.user.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mhys.hms.entity.Department;
import com.mhys.hms.mapper.DeptManagerMapper;
import com.mhys.hms.service.user.DeptManagerService;

/**
 * 部门管理业务类
 * @author 
 *
 */
@Service
public class DeptManagerServiceImpl implements DeptManagerService{
	@Autowired
	private DeptManagerMapper deptManagerMapper;
	
	/**
	 * 分页模糊查询部门信息
	 */
	@Override
	public List<Department> getDeptInfo(int pageSize, int pageNumber,String deptNo,String deptName) {
		int startNum = (pageNumber - 1) * pageSize;
		return deptManagerMapper.getDeptInfo(startNum, pageSize,deptNo,deptName);
	}
	/**
	 * 根据id删除id
	 */
	@Override
	public Integer delDeptInfo(String deptId) {
		//根据id查询部门信息
		List<Department> list = deptManagerMapper.getDeptInfoById(Integer.parseInt(deptId));
		//判断部门的人数,如果大于0，则不能删除并提示
		if(list.get(0).getPersonNum()>0) {
			return -1;
		}
		
		return deptManagerMapper.delDeptInfo(deptId);
	}
	/**
	 * 新增或者更新部门信息
	 */
	@Override
	public Integer addDeptInfo(Department dept) {
		if(dept.getId() == null) {//如果id为null，则是新增
			int row = 0;
			
			//首先查找部门是否存在
			List<Department> list = deptManagerMapper.queryDeptByName(dept.getDepartmentName());
			//如果存在，就不增加
			if(list.size()>0) {
				row = -1;
			}else {
				row = deptManagerMapper.addDeptInfo(dept);
			}
			return row;
		}else {//否则就是修改
			return deptManagerMapper.updateDeptInfo(dept);
		}
	}
	@Override
	public List<Department> getDeptInfoById(Integer id) {
		return deptManagerMapper.getDeptInfoById(id);
	}
	@Override
	public int getTotalNum(int pageSize, int pageNumber, String deptNo, String deptName) {
		int startNum = (pageNumber - 1) * pageSize;
		return deptManagerMapper.getTotalNum(startNum, pageSize, deptNo, deptName);
	}
	@Override
	public List<String> queryAllDeptName() {
		return deptManagerMapper.queryAllDeptName();
	}
	@Override
	public List<Department> queryDeptByName(String deptName) {
		return deptManagerMapper.queryDeptByName(deptName);
	}

}
